package com.hu.zhcc.shiro.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hu.zhcc.shiro.dao.CheckHabitusMapper;
import com.hu.zhcc.shiro.dao.CheckResultMapper;
import com.hu.zhcc.shiro.entity.bo.CheckGroup;
import com.hu.zhcc.shiro.entity.bo.CheckHabitus;
import com.hu.zhcc.shiro.entity.bo.CheckResult;
import com.hu.zhcc.shiro.entity.vo.CheckGroupVO;
import com.hu.zhcc.shiro.entity.vo.CheckHabitusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CheckHabitusServiceImpl implements CheckHabitusService{

    @Autowired
    private CheckHabitusMapper checkHabitusMapper;
    @Autowired
    private CheckResultMapper checkResultMapper;

    @Override
    public boolean AddHabitus(CheckHabitus checkHabitus) {
        if(checkHabitus.getFilenumber()==null || checkHabitus.getFilenumber()=="" ){
            checkHabitusMapper.insertSelective(checkHabitus);
            CheckHabitus habitus = checkHabitusMapper.selectByPrimaryKey(checkHabitus.getId());
            String  valuationdate = habitus.getValuationdate();
            String[] strings = valuationdate.split("-");
            String newnumber=strings[0]+strings[1]+strings[2]+"00"+checkHabitus.getId();
            habitus.setFilenumber(newnumber);
            return checkHabitusMapper.updateByPrimaryKey(habitus)>0;
        }else {
            return checkHabitusMapper.insert(checkHabitus)>0;
        }
    }

    @Override
    public List<CheckHabitus> ShowHabitus() {
        return checkHabitusMapper.show();
    }

    @Override
    public CheckResult findResult(Short id) {
        return checkResultMapper.selectByPrimaryKey(id);
    }
    //分页查询
    @Override
    public PageInfo<CheckHabitus> pageQuery(CheckHabitusVO checkHabitusVO) {
        PageHelper.startPage(checkHabitusVO.getPageNum(),checkHabitusVO.getPageSize());
        List<CheckHabitus> byCondition = checkHabitusMapper.findByCondition(checkHabitusVO.getQueryString());
        PageInfo<CheckHabitus> checkItemPageInfo = new PageInfo<>(byCondition);
        return checkItemPageInfo;
    }

    @Override
    public boolean updateHabitus(CheckHabitus checkHabitus) {
        return checkHabitusMapper.updateByPrimaryKey(checkHabitus)>0;
    }

    @Override
    public boolean removeHabitus(Short id) {
        return checkHabitusMapper.deleteByPrimaryKey(id)>0;
    }
}
